package com.alatus;

import com.alatus.utils.GameUtil;
import java.awt.*;

/**
 * 顶层地图类
 * 绘制顶层
 */
public class MapTop {
//    格子位置
    private int temp_x;
    private int temp_y;
    //    重置游戏
    void reGame(){
        for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
            for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
                GameUtil.DATA_TOP[i][j] = 0;
            }
        }
    }
    private void logic(){
        temp_x = 0;
        temp_y = 0;
        if(GameUtil.MOUSE_X > GameUtil.OFFSET && GameUtil.MOUSE_Y > GameUtil.OFFSET * 3){
            temp_x = (GameUtil.MOUSE_X - GameUtil.OFFSET) / GameUtil.SQUARE_LENGTH + 1;
            temp_y = (GameUtil.MOUSE_Y - GameUtil.OFFSET * 3) / GameUtil.SQUARE_LENGTH + 1;
        }
        if(temp_x >= 1 && temp_x <= GameUtil.MAP_WIDTH && temp_y >= 1 && temp_y <= GameUtil.MAP_HEIGHT){
            if(GameUtil.LEFT){
//                如果当前是覆盖,则翻开
                if(GameUtil.DATA_TOP[temp_x][temp_y]==0){
                    GameUtil.DATA_TOP[temp_x][temp_y] = -1;
                }
                spaceOpen(temp_x,temp_y);
                GameUtil.LEFT = false;
            }
            if (GameUtil.RIGHT){
//                如果当前是覆盖,则插旗
                if(GameUtil.DATA_TOP[temp_x][temp_y]==0){
                    GameUtil.DATA_TOP[temp_x][temp_y] = 1;
                    GameUtil.FLAG_NUM++;
                }
//                当前是插旗,则取消插旗
                else if(GameUtil.DATA_TOP[temp_x][temp_y]==1){
                    GameUtil.DATA_TOP[temp_x][temp_y] = 0;
                    GameUtil.FLAG_NUM--;
                }
                else if(GameUtil.DATA_TOP[temp_x][temp_y] == -1){
                   numOpen(temp_x,temp_y);
                }
                GameUtil.RIGHT = false;
            }
        }
        boom();
        victory();
    }
//    失败判定
    private boolean boom(){
//        如果旗子数等于炸弹的总数
        if(GameUtil.FLAG_NUM == GameUtil.MINE_MAX){
            for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
                for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
//                    如果底层为雷,而顶层无覆盖,则判断游戏失败
                    if(GameUtil.DATA_BOTTOM[i][j] == 0){
                        GameUtil.DATA_TOP[i][j] = -1;
                    }
                }
            }
        }
        for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
            for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
//                如果底层为雷,而顶层无覆盖,则判断游戏失败
                if(GameUtil.DATA_BOTTOM[i][j] == -1 && GameUtil.DATA_TOP[i][j] == -1){
                    GameUtil.state = 2;
                    showMine();
                    return true;
                }
            }
        }
        return false;
    }
//    胜利判断
    private boolean victory(){
        int count = 0;
        for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
            for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
                if(GameUtil.DATA_TOP[i][j] != -1){
                    count++;
                }
            }
        }
        if(count == GameUtil.MINE_MAX){
            GameUtil.state = 1;
            for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
                for (int j = 1; j < GameUtil.MAP_HEIGHT; j++) {
//                    未翻开的都变成旗子
                    if(GameUtil.DATA_TOP[i][j] == 0){
                        GameUtil.DATA_TOP[i][j] = 1;
                    }
                }
            }
            return true;
        }
        return false;
    }
//    失败了给用户看所有的地雷
    private void showMine(){
        for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
            for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
//                如果底层是雷,而顶层不是旗子,则显示
                if(GameUtil.DATA_BOTTOM[i][j] == -1 && GameUtil.DATA_TOP[i][j] != -1){
                    GameUtil.DATA_TOP[i][j] = -1;
                }
//                底层不是雷,顶层是旗,显示插错旗
                if(GameUtil.DATA_BOTTOM[i][j] != -1 && GameUtil.DATA_TOP[i][j] == 1){
                    GameUtil.DATA_TOP[i][j] = 2;
                }
            }
        }
    }
//    数字翻开
    private void numOpen(int x,int y){
//        记录旗子数量
        int count = 0;
        if(GameUtil.DATA_BOTTOM[x][y] > 0){
            for (int i = x-1; i <= x+1; i++) {
                for (int j = y-1; j <= y+1; j++) {
                    if(GameUtil.DATA_TOP[i][j] == 1){
                        count++;
                    }
                }
            }
            if(count == GameUtil.DATA_BOTTOM[x][y]){
                for (int i = x - 1; i < x + 1; i++) {
                    for (int j = y - 1; j <= y+1; j++) {
                        if(GameUtil.DATA_TOP[i][j] != 1){
                            GameUtil.DATA_TOP[i][j] = -1;
                        }
//                        必须在雷区中
                        if(i>=1 && i<=GameUtil.MAP_HEIGHT && j>=1 && j<=GameUtil.MAP_HEIGHT){
                            spaceOpen(i,j);
                        }
                    }
                }
            }
        }
    }
//    打开空格
    private void spaceOpen(int x,int y){
        if(GameUtil.DATA_BOTTOM[x][y] == 0){
            for (int i = x-1; i <= x+1; i++) {
                for (int j = y-1; j <= y+1; j++) {
//                    覆盖才递归
                    if(GameUtil.DATA_TOP[i][j] != -1){
//                        翻开时如果有旗子,释放这个旗子
                        if(GameUtil.DATA_TOP[i][j] == 1){
                            GameUtil.FLAG_NUM--;
                        }
                        GameUtil.DATA_TOP[i][j] = -1;
//                        必须在雷区中
                        if(i>=1 && i<=GameUtil.MAP_HEIGHT && j>=1 && j<=GameUtil.MAP_HEIGHT){
                            spaceOpen(i,j);
                        }
                    }
                }
            }
        }
    }
    public void paintSelf(Graphics graphics){
        logic();
        for (int i = 1; i <= GameUtil.MAP_WIDTH; i++) {
            for (int j = 1; j <= GameUtil.MAP_HEIGHT; j++) {
//                覆盖
                if(GameUtil.DATA_TOP[i][j] == 0){
                    graphics.drawImage(GameUtil.top,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                }
//                插旗
                if(GameUtil.DATA_TOP[i][j] == 1){
                    graphics.drawImage(GameUtil.flag,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                }
//                插错旗
                if(GameUtil.DATA_TOP[i][j] == 2){
                    graphics.drawImage(GameUtil.wrongFlag,
                            GameUtil.OFFSET + (i - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.OFFSET * 3 + (j - 1) * GameUtil.SQUARE_LENGTH + 1,
                            GameUtil.SQUARE_LENGTH - 2,
                            GameUtil.SQUARE_LENGTH - 2,
                            null);
                }
            }
        }
    }
}
